package com.chao.common.codeGeneration.source;

import com.chao.common.codeGeneration.bean.Table;
import com.chao.common.codeGeneration.bean.TableColumns;
import com.chao.common.codeGeneration.generation.FileUtile;
import com.chao.common.codeGeneration.generation.Generation;
import com.chao.common.util.DateUtile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

public class ToBean {

    public static void toBean(Table table, String path){
        File file = FileUtile.createFile(path+"\\bean\\"+table.getPojoKey()+".java");
        try(Writer out =new FileWriter(file)) {
            StringBuilder sb = new StringBuilder();
            List<String> fun = Arrays.asList(table.getFunction());
            // package
            sb.append("package com.chao.").append(table.getService()).append(".modular.").append(table.getPacName()).append(".bean;").append("\r\n\r\n");

            //import
            sb.append("import lombok.Data;").append("\r\n");
            if(fun.contains("附件")) {
                sb.append("import java.util.List;").append("\r\n");
                sb.append("import com.chao.common.util.file.FileUpload;").append("\r\n");
            }
            sb.append("import com.chao.common.util.reaction.Reaction;").append("\r\n");

            String importDate = null;
            String importBigDecimal = null;
            String importBigInteger = null;

            //解析字段
            StringBuilder columns = new StringBuilder();
            for(TableColumns column: table.getColumns()){
                if("Date".equals(column.getBeanType())){
                    importDate = "import java.util.Date;";
                }else if("BigDecimal".equals(column.getBeanType())){
                    importBigDecimal = "import java.math.BigDecimal;";
                }else if("BigInteger".equals(column.getBeanType())){
                    importBigInteger = "import java.math.BigInteger;";
                }
                columns.append("\r\n");
                columns.append("\t/**").append("\r\n");
                columns.append("\t* ").append(column.getColumnComment()).append("\r\n");
                columns.append("\t*/").append("\r\n");
                columns.append("\tprivate ").append(column.getBeanType()).append(" ").append(column.getLowercaseName()).append(";").append("\r\n");
            }
            if(fun.contains("附件")) {
                columns.append("\r\n");
                columns.append("\t/**").append("\r\n");
                columns.append("\t* ").append("附件列表").append("\r\n");
                columns.append("\t*/").append("\r\n");
                columns.append("\tprivate List<FileUpload> fileList;\r\n");
            }

            if(null != importDate){
                sb.append(importDate).append("\r\n");
            }
            if(null != importBigDecimal){
                sb.append(importBigDecimal).append("\r\n");
            }
            if(null != importBigInteger){
                sb.append(importBigInteger).append("\r\n");
            }

            //Class
            sb.append("\r\n");
            sb.append("\r\n");
            sb.append("/**").append("\r\n");
            sb.append(" * ").append(table.getPojoName()).append("\r\n");
            sb.append(" * ").append(Generation.author).append("\r\n");
            sb.append(" * ").append(DateUtile.toYmd()).append("\r\n");
            sb.append(" */").append("\r\n");
            sb.append("@Data").append("\r\n");
            sb.append("public class ").append(table.getPojoKey()).append(" implements Reaction {").append("\r\n");
            sb.append("\r\n");
            sb.append(columns);
            sb.append("\r\n");
            sb.append("}");
            out.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
